/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.jstester;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

import junit.framework.TestCase;

/**
 * TestCase for javascript code validation.<br>
 * This class is a wrapper on JsTester.
 * 
 * @author Andres Almiray <aalmiray@users.sourceforge.net>
 */
public abstract class JsTestCase extends TestCase {
   private JsTester jsTester;

   public JsTestCase() {
      super();
   }

   public JsTestCase( String testName ) {
      super( testName );
   }

   /**
    * Asserts if two arrays are equal.
    */
   public void assertArrayEquals( String expr1, String expr2 ) {
      jsTester.assertArrayEquals( expr1, expr2 );
   }

   /**
    * Asserts if two arrays are equal.
    */
   public void assertArrayEquals( String msg, String expr1, String expr2 ) {
      jsTester.assertArrayEquals( msg, expr1, expr2 );
   }

   /**
    * Asserts that a binary predicate is true.<br>
    * The predicate can not be null.
    */
   public void assertBinaryPredicate( String predicate, String expr1, String expr2 ) {
      jsTester.assertBinaryPredicate( predicate, expr1, expr2 );
   }

   /**
    * Asserts that a binary predicate is true.<br>
    * The predicate can not be null.
    */
   public void assertBinaryPredicate( String msg, String predicate, String expr1, String expr2 ) {
      jsTester.assertBinaryPredicate( msg, predicate, expr1, expr2 );
   }

   /**
    * Asserts that two expressions are equal.<br>
    * If the expressions are arrays, calls assertArrayEquals.<br>
    * If the expressions are objects, calls assertObjectEquals.<br>
    * This method calls JsTester.assertEquals()
    */
   public void assertExpressionEquals( String expr1, String expr2 ) {
      jsTester.assertEquals( expr1, expr2 );
   }

   /**
    * Asserts that two expressions are equal.<br>
    * If the expressions are arrays, calls assertArrayEquals.<br>
    * If the expressions are objects, calls assertObjectEquals.<br>
    * This method calls JsTester.assertEquals()
    */
   public void assertExpressionEquals( String msg, String expr1, String expr2 ) {
      jsTester.assertEquals( msg, expr1, expr2 );
   }

   public void assertIsAlien( String expr ) {
      jsTester.assertIsAlien( expr );
   }

   public void assertIsAlien( String msg, String expr ) {
      jsTester.assertIsAlien( msg, expr );
   }

   /**
    * Asserts that the expression is an array.
    */
   public void assertIsArray( String expr ) {
      jsTester.assertIsArray( expr );
   }

   /**
    * Asserts that the expression is an array.
    */
   public void assertIsArray( String msg, String expr ) {
      jsTester.assertIsArray( msg, expr );
   }

   /**
    * Asserts that the expression is a boolean.
    */
   public void assertIsBoolean( String expr ) {
      jsTester.assertIsBoolean( expr );
   }

   /**
    * Asserts that the expression is a boolean.
    */
   public void assertIsBoolean( String msg, String expr ) {
      jsTester.assertIsBoolean( msg, expr );
   }

   /**
    * Asserts that the expression an empty object.
    */
   public void assertIsEmpty( String expr ) {
      jsTester.assertIsEmpty( expr );
   }

   /**
    * Asserts that the expression an empty object.
    */
   public void assertIsEmpty( String msg, String expr ) {
      jsTester.assertIsEmpty( msg, expr );
   }

   /**
    * Asserts that the expression is a function.
    */
   public void assertIsFunction( String expr ) {
      jsTester.assertIsFunction( expr );
   }

   /**
    * Asserts that the expression is a function.
    */
   public void assertIsFunction( String msg, String expr ) {
      jsTester.assertIsFunction( msg, expr );
   }

   /**
    * Asserts that the expression is a number.
    */
   public void assertIsNumber( String expr ) {
      jsTester.assertIsNumber( expr );
   }

   /**
    * Asserts that the expression is a number.
    */
   public void assertIsNumber( String msg, String expr ) {
      jsTester.assertIsNumber( msg, expr );
   }

   /**
    * Asserts that the expression is an object.
    */
   public void assertIsObject( String expr ) {
      jsTester.assertIsObject( expr );
   }

   /**
    * Asserts that the expression is an object.
    */
   public void assertIsObject( String msg, String expr ) {
      jsTester.assertIsObject( msg, expr );
   }

   /**
    * Asserts that the expression is a string.
    */
   public void assertIsString( String expr ) {
      jsTester.assertIsString( expr );
   }

   /**
    * Asserts that the expression is a string.
    */
   public void assertIsString( String msg, String expr ) {
      jsTester.assertIsString( msg, expr );
   }

   /**
    * Asserts that the expression is undefined.
    */
   public void assertIsUndefined( String expr ) {
      jsTester.assertIsUndefined( expr );
   }

   /**
    * Asserts that the expression is undefined.
    */
   public void assertIsUndefined( String msg, String expr ) {
      jsTester.assertIsUndefined( msg, expr );
   }

   /**
    * Asserts that the expression is not null.
    */
   public void assertNotNull( String expr ) {
      jsTester.assertNotNull( expr );
   }

   /**
    * Asserts that the expression is not null.
    */
   public void assertNotNull( String msg, String expr ) {
      jsTester.assertNotNull( msg, expr );
   }

   /**
    * Asserts that the expression is null.
    */
   public void assertNull( String expr ) {
      jsTester.assertNull( expr );
   }

   /**
    * Asserts that the expression is null.
    */
   public void assertNull( String msg, String expr ) {
      jsTester.assertNull( msg, expr );
   }

   /**
    * Asserts that the two expressions are objects and equal.<br>
    * If any property is an array, calls assertArrayEquals.<br>
    * If any property is an object, calls assertObjectEquals.<br>
    */
   public void assertObjectEquals( String expr1, String expr2 ) {
      jsTester.assertObjectEquals( expr1, expr2 );
   }

   /**
    * Asserts that the two expressions are objects and equal.<br>
    * If any property is an array, calls assertArrayEquals.<br>
    * If any property is an object, calls assertObjectEquals.<br>
    */
   public void assertObjectEquals( String msg, String expr1, String expr2 ) {
      jsTester.assertObjectEquals( msg, expr1, expr2 );
   }

   /**
    * Asserts that the expression is true.
    */
   public void assertTrue( String expr ) {
      jsTester.assertTrue( expr );
   }

   /**
    * Asserts that the expression is true.
    */
   public void assertTrue( String msg, String expr ) {
      jsTester.assertTrue( msg, expr );
   }

   /**
    * Asserts that a unary predicate is true.<br>
    * The predicate can not be null.
    */
   public void assertUnaryPredicate( String predicate, String expr ) {
      jsTester.assertUnaryPredicate( predicate, expr );
   }

   /**
    * Asserts that a unary predicate is true.<br>
    * The predicate can not be null.
    */
   public void assertUnaryPredicate( String msg, String predicate, String expr ) {
      jsTester.assertUnaryPredicate( msg, predicate, expr );
   }

   /**
    * Executes an arbitrary expression.<br>
    * It fails if the expression throws a JsAssertException.<br>
    * It fails if the expression throws a RhinoException.<br>
    */
   public Object eval( String expr ) {
      return jsTester.eval( expr );
   }

   /**
    * Get the current context.
    */
   public Context getContext() {
      return jsTester.getContext();
   }

   /**
    * Get the global scope.
    */
   public Scriptable getGlobalScope() {
      return jsTester.getGlobalScope();
   }

   /**
    * Loads a script available in the classpath.
    */
   public String loadScript( String scriptName ) {
      return JsTester.loadScript( scriptName );
   }

   /**
    * Loads a script available in the classpath.
    */
   public String loadScript( String scriptName, String encoding ) {
      return JsTester.loadScript( scriptName, encoding );
   }

   /**
    * Initializes the tester.
    */
   protected void setUp() throws Exception {
      jsTester = new JsTester();
      jsTester.onSetUp();
   }

   /**
    * Cleanups the tester.
    */
   protected void tearDown() throws Exception {
      jsTester.onTearDown();
   }
}
